<?php

class MCourse extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
	
	function getCourseById($id){
		$data = array();
	    $this->db->where('course_id',id_clean($id));
	    $this->db->where('uid',id_clean($_SESSION['uid']));
	    $this->db->limit(1);
	    $Q = $this->db->get('course');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
	 
	function getCourses(){
		$data = array();
	    $this->db->where('uid',id_clean($_SESSION['uid']));
	    $this->db->order_by("title", "ASC");
	    $Q = $this->db->get('course');
	    if ($Q->num_rows() > 0){
	    	foreach ($Q->result_array() as $row){
	        	 $data[] = $row;
	      	 }
	    }
	    $Q->free_result();  
	    return $data;   
	}
	
	function getCourseName($coursename)
	{
		$data = array();
	    $this->db->where('title',$coursename);
	    $this->db->limit(1);
	    $Q = $this->db->get('course');
	    
		if ($Q->num_rows() > 0){
	      	$data = $Q->row_array();
	    }
	    
	    $Q->free_result();  
	    return $data;
	}
	 
	function getCourseByUID($uid){
		$data = array();
		$this->db->select('course_id,title');
	    $this->db->where('uid',id_clean($uid));
	    $this->db->order_by("title", "ASC");
	    $Q = $this->db->get('course');
	    if ($Q->num_rows() > 0){
	      foreach ($Q->result_array() as $row){
	         $data[$row['course_id']] = $row['title'];
	       }
	    }
	    $Q->free_result();  
	    return $data;   
	 }
		
	 function addCourse(){
		$data = array( 
			'title' => db_clean($_POST['title']),
			'shortdesc' => db_clean($_POST['shortdesc']),
			'longdesc' => db_clean($_POST['longdesc']),
			'uid' => db_clean($_SESSION['uid'])
		);
		$this->db->insert('course', $data);	 
	 }
	 
	 function updateCourse(){
		$data = array( 
			'title' => db_clean($_POST['title']),
			'shortdesc' => db_clean($_POST['shortdesc']),
			'longdesc' => db_clean($_POST['longdesc']),
			'uid' => db_clean($_SESSION['uid'])
		);
		$this->db->where('course_id',id_clean($_POST['course_id']));
	    $this->db->where('uid',id_clean($_SESSION['uid']));
		$this->db->update('course', $data);	
	 
	 }
	 
	 function deleteCourse($id){
	 	$this->db->where('course_id', id_clean($id));
	 	$this->db->where('uid',id_clean($_SESSION['uid']));
		$this->db->delete('course');	
	 }
	 
	function getResourcesByCourse($courseid){
	     $data = array();
	     $this->db->select('file.*,file_type.type as file_type, shift.title as shift, user.username');
	     $this->db->from('file');
	     $this->db->join('file_type','file.file_type_id = file_type.file_type_id');
	     $this->db->join('shift', 'file.shift_id = shift.shift_id');
	     $this->db->join('user', 'file.uid = user.uid');
	     $this->db->where('file.course_id',id_clean($courseid));
	     $this->db->where('file.activate',1);
	     $this->db->order_by("file_type.type", "ASC");
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	}
}

?>